package restful.interceptor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;

import org.jboss.resteasy.core.Headers;
import org.jboss.resteasy.core.ResourceMethodInvoker;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

import restful.annotation.BoxPower;
import restful.bean.Result;


@SuppressWarnings("deprecation")
public class Interceptor4PreProcess implements PreProcessInterceptor {
	
	@Context HttpServletRequest request;

	@Override
	public ServerResponse preProcess(HttpRequest httpRequest, ResourceMethodInvoker resourceMethodInvoker) {
		Method method = resourceMethodInvoker.getMethod();
		 if (method.isAnnotationPresent(BoxPower.class)) {
			    BoxPower annotation = method.getAnnotation(BoxPower.class);
				String boxPowerValue = ((BoxPower)annotation).value();
				if (request.getSession().getAttribute("username")==null) {
					Result result = new Result(-20, "无名之辈也敢来挑战？", "/box/ini");
					return new ServerResponse(result, 200, new Headers<Object>());
				}
				else {
					if (boxPowerValue.equals("box1")) {
					HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
					String box1Value = powerList.get("box1");
						if (box1Value==null) {
							Result result = new Result(-20, "无名之辈也敢来挑战？", "/box/ini");
							return new ServerResponse(result, 200, new Headers<Object>());
							}
					}else if (boxPowerValue.equals("box2")) {
						HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
						String box2Value = powerList.get("box2");
						if (box2Value==null) {
							Result result = new Result(-10, "你想投机取巧，还是乖乖地按顺序回答我的问题！", "");
							return new ServerResponse(result, 200, new Headers<Object>());
							}
					}else if (boxPowerValue.equals("box3")) {
						HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
						String box3Value = powerList.get("box3");
						if (box3Value==null) {
							Result result = new Result(-10, "你想投机取巧，还是乖乖地按顺序回答我的问题！", "");
							return new ServerResponse(result, 200, new Headers<Object>());
							}
					}else if (boxPowerValue.equals("box4")) {
						HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
						String box4Value = powerList.get("box4");
						if (box4Value==null) {
							Result result = new Result(-10, "你想投机取巧，还是乖乖地按顺序回答我的问题！", "");
							return new ServerResponse(result, 200, new Headers<Object>());
							}
					}else if (boxPowerValue.equals("box5")) {
						HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
						String box5Value = powerList.get("box5");
						if (box5Value==null) {
							Result result = new Result(-10, "你想投机取巧，还是乖乖地按顺序回答我的问题！", "");
							return new ServerResponse(result, 200, new Headers<Object>());
							}
					}else if (boxPowerValue.equals("finish")) {
						HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
						String box6Value = powerList.get("box6");
						if (box6Value==null) {
							Result result = new Result(-10, "你想投机取巧，还是乖乖地按顺序回答我的问题！", "");
							return new ServerResponse(result, 200, new Headers<Object>());
							}
					}				
					else {
						Result result = new Result(-10, "你想投机取巧，还是乖乖地按顺序回答我的问题！", "");
						return new ServerResponse(result, 200, new Headers<Object>());
					}
				}
		}
		return null;
	}

}
